package petrus.components
{

	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import petrus.components.properties.Properties;


	public class AbstractComponent extends QuartzComponent implements IComponent
	{
	

		private var _properties:Properties;


		public function AbstractComponent()
		{
			properties = new Properties();
			_build();

			if (stage)
				_init();
			else
				addEventListener(Event.ADDED_TO_STAGE, _init);
		}



		/**
		 * Fonction de resize.
		 */
		public function resize(width:Number=0, height:Number=0):void
		{

		}


		/**
		 * Lance l'apparition de l'élément
		 */
		public function transitionIn(delay:Number=0):void
		{

		}


		/**
		 * Exécuté lors de la fin de l'apparition.
		 */
		public function transitionInComplete():void
		{
		}


		/**
		 * Lance la disparition de l'élement.
		 */
		public function transitionOut(delay:Number=0):void
		{
		}


		/**
		 * Exécuté lors de la fin de la disparition.
		 */
		public function transitionOutComplete():void
		{
		}


		/**
		 *
		 * @param	rectangle
		 * @param	color
		 */
		public function drawShape(rectangle:Rectangle=null, color:uint=0x000000, opacity:Number=1):Shape
		{
			var shape:Shape = new Shape();
			shape.graphics.clear();
			shape.graphics.beginFill(color, opacity);
			shape.graphics.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
			shape.graphics.endFill();
			return shape;
		}


		/**
		 *
		 * @param	rectangle
		 * @param	color
		 */
		public function drawSprite(rectangle:Rectangle=null, color:uint=0x000000, opacity:Number=1):Sprite
		{
			var sprite:Sprite = new Sprite();
			sprite.graphics.clear();
			sprite.graphics.beginFill(color, opacity);
			sprite.graphics.drawRect(rectangle.x, rectangle.y, rectangle.width, rectangle.height);
			sprite.graphics.endFill();
			return sprite;
		}


		/**
		 *
		 * @return
		 */
		public function createTextField(color:uint=0x000000):TextField
		{
			var textfield:TextField = new TextField();
			textfield.textColor = color;
			textfield.embedFonts = false;
			textfield.selectable = false;
			textfield.autoSize = TextFieldAutoSize.LEFT;
			return textfield;
		}



		public function get properties():Properties
		{
			return _properties;
		}


		public function set properties(source:Properties):void
		{
			_properties = source;
		}


		/**
		 * Fonction de destruction ( auto remove le composant si il a un parent ).
		 */
		public function destroy(e:Event=null):void
		{
			if (e != null && hasEventListener(e.type))
				removeEventListener(e.type, arguments.callee);
		}






		/**
		 * Fonction de construction.
		 */
		protected function _build(params:*=null, optionals:*=null):void
		{

		}


		/**
		 * Fonction d'initialisation.
		 */
		protected function _init(e:Event=null):void
		{
			/*
			   if (e != null && hasEventListener(e.type))
			 removeEventListener(e.type, arguments.callee)*/

			if (e)
				removeEventListener(Event.ADDED_TO_STAGE, _init)

			addEventListener(Event.REMOVED_FROM_STAGE, destroy);
		}
	}
}